package com.example.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import com.example.common.constant.BizCodeEnum;
import com.example.common.vo.UserLoginVo;
import com.example.common.vo.UserRegisterVo;
import com.example.gulimall.member.feign.CouponFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.common.entity.member.MemberEntity;
import com.example.gulimall.member.service.MemberService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 会员
 *
 * @author zyred
 * @email zyred@163.com
 * @date 2021-08-23 11:06:50
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

    @Autowired private MemberService memberService;
    @Autowired private CouponFeignClient couponFeignClient;

    @GetMapping("/coupons")
    public R getCoupons () {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        return this.couponFeignClient.memberCoupons().put("member", memberEntity);
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);
        return R.ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/register")
    public R register(@RequestBody UserRegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo vo){
        String json = this.memberService.login(vo);
        return Objects.nonNull(json)
                ? R.ok().put("data", json)
                : R.error(BizCodeEnum.USER_PASSWORD_ERROR.getCode(), BizCodeEnum.USER_PASSWORD_ERROR.getMsg());
    }

    @PostMapping("/isRegister")
    MemberEntity isRegister(@RequestBody MemberEntity userInfo){
        return this.memberService.isRegister (userInfo);
    }
}
